//
// @Auther: by wujehy 
// @Email:wujehy@qq.com
// @Data:2020/9/14
// @Time:11:39 上午
//
//

#include <base_types.h>
#include "KeyValueMod.h"
#include "glog/logging.h"

using namespace GeeJoan;

void KeyValueMod::init()
{
    SubTypeFunction set_func = [&](const HanderDataType *hander, StatusCodeTypeEnum &code, std::string &resposeBody)
    {
        // 处理 set 子业务
        uint8_t keylen = hander->message.at(0);
        std::string key = hander->message.substr(1, keylen);
        uint8_t valuelen = hander->message.at(1 + keylen);
        std::string value = hander->message.substr(2 + keylen, 2 + keylen + valuelen);

        m_key_value_map.insert({key, value});

        code = Status_SUCCESS;
    };

    SubTypeFunction get_func = [&](const HanderDataType *hander, StatusCodeTypeEnum &code, std::string &resposeBody)
    {
        // 处理 set 子业务
        auto find = m_key_value_map.find(hander->message);

        if (find != m_key_value_map.end())
        {
            // 找到
            code = Status_SUCCESS;
            resposeBody = find->second;

        } else
        {
            // 不存在
            code = Status_FAIL;
        }
    };


    m_sub_type_func_map.insert({OpKeyValueModSubType_set, set_func});
    m_sub_type_func_map.insert({OpKeyValueModSubType_get, get_func});
}

//void KeyValueMod::init_complete(Global_Context *context)
//{
//    LOGINFO << " KeyValueMod::init_complete ";
//    this->globalContext = context;
//}

ModTypeEnum KeyValueMod::mod_type()
{
    return ModType_KeyValueMod;
}

void KeyValueMod::procMessage(const HanderDataType *hander, StatusCodeTypeEnum &code, std::string &resposeBody)
{
    auto findSubFunc = m_sub_type_func_map.find(hander->subtype);
    if (findSubFunc != m_sub_type_func_map.end())
    {
        findSubFunc->second(hander, code, resposeBody);
    } else
    {
        LOGINFO << " not find sub fun subtype =" << hander->subtype;
    }
}
